import operLogService from "@/service/operLogs.service";
import { Context } from "koa";
import { Next } from "koa";
import { body, middlewares, routeConfig, z } from "koa-swagger-decorator";
import authMiddleware from "@/middleware/auth.middleware";

// 添加日志参数模式
const addSchema = z.object({
  title: z.string(),
  business_type: z.coerce.number(),
  method: z.string(),
  request_method: z.string(),
  oper_name: z.string(),
  oper_url: z.string(),
  oper_ip: z.string(),
  oper_param: z.string().nullable().optional(),
  json_result: z.string().nullable().optional(),
  status: z.coerce.number(),
  error_msg: z.string().nullable().optional(),
});

// 删除日志参数模式
const deleteSchema = z.object({
  ids: z.array(z.number()),
});

// 获取日志列表参数模式
const getListSchema = z.object({
  current: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).default(10),
  oper_name: z.string().nullable().optional(),
  title: z.string().nullable().optional(),
  status: z.coerce.number().nullable().optional(),
  startTime: z.string().nullable().optional(),
  endTime: z.string().nullable().optional(),
});

export class OperLogController {
  // 添加操作日志
  @routeConfig({
    method: "post",
    path: "/operLog/add",
    summary: "添加操作日志",
    tags: ["操作日志"],
  })
  @body(addSchema)
  @middlewares([authMiddleware.verifyAuth])
  async add(ctx: Context, next: Next) {
    try {
      const res = await operLogService.add(
        ctx.request.body as API.OperLogParams
      );
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "添加操作日志失败",
      };
      ctx.status = 400;
    }
  }

  // 批量删除操作日志
  @routeConfig({
    method: "delete",
    path: "/operLog/delete",
    summary: "批量删除操作日志",
    tags: ["操作日志"],
  })
  @body(deleteSchema)
  @middlewares([authMiddleware.verifyAuth])
  async delete(ctx: Context, next: Next) {
    try {
      const res = await operLogService.delete(
        (ctx.request.body as { ids: number[] }).ids
      );
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "删除操作日志失败",
      };
      ctx.status = 400;
    }
  }

  // 清空操作日志
  @routeConfig({
    method: "delete",
    path: "/operLog/clean",
    summary: "清空操作日志",
    tags: ["操作日志"],
  })
  @middlewares([authMiddleware.verifyAuth])
  async clean(ctx: Context, next: Next) {
    try {
      const res = await operLogService.clean();
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "清空操作日志失败",
      };
      ctx.status = 400;
    }
  }

  // 获取操作日志列表
  @routeConfig({
    method: "get",
    path: "/operLog/list",
    summary: "获取操作日志列表",
    tags: ["操作日志"],
    request: {
      query: getListSchema,
    },
  })
  @middlewares([authMiddleware.verifyAuth])
  async getList(ctx: Context, next: Next) {
    try {
      const res = await operLogService.getList(
        ctx.request.query as unknown as API.OperLogList
      );
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "获取操作日志列表失败",
      };
      ctx.status = 400;
    }
  }
}

export default new OperLogController();
